import { getPlayerData, isPlayerExist, storagePlayerData } from '../l.js'
export class Fs extends plugin {
    constructor() {
        super({
            name: '[Fs]购买农田',
            dsc: '购买农田',
            event: 'message',
            priority: 1,
            rule: [
                { reg: /^(#|\/)?(购买|添置)(农田|田地)$/, fnc: 'purchaseFarmland' }
            ]
        })
    }
    async purchaseFarmland(e) {
        const PLAYER_ID = e.user_id
        if (!isPlayerExist(PLAYER_ID)) {
            return e.reply(['无玩家信息，请发送[领取田地]'])
        }
        const PLAYER_DATA = await getPlayerData(PLAYER_ID)
        const NUMBER_OF_FARMLAND = PLAYER_DATA['frame'].length

        if (NUMBER_OF_FARMLAND >= 36) return e.reply(['田地上限，已不可购买'])

        let price = 0
        for (let i = 0; i < NUMBER_OF_FARMLAND; i++) {
            price += 1000 * NUMBER_OF_FARMLAND
        }

        if (PLAYER_DATA['capital'] < price) return e.reply([`💰不足，无法购买农田\r下一块农田需要[${price}]💰喵~`])

        PLAYER_DATA['frame'][NUMBER_OF_FARMLAND] = { start: false, name: "无", time: "无" }
        PLAYER_DATA['capital'] -= price
        storagePlayerData(PLAYER_ID, PLAYER_DATA)

        const msg = [
            '购买成功!',
            `这是您的第${NUMBER_OF_FARMLAND + 1}块田喵~`
        ].join('\r')
        return e.reply(msg)
    }
}